<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta HTTP-EQUIV="pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<meta HTTP-EQUIV="expires" CONTENT="0">
<title>_商品管理</title>
<script type="text/javascript">
		$(function(){
			$("#queryForm").validate(); 
		});
		
		var filterParam = {};
		var datatable_content;
		function initTable(){
			datatable_content = $("#contentTable").dataTable({
			   "bServerSide":true,
			   "bAutoWidth":false,
	           "sAjaxSource":'${ctx}/goods/page',
	           "fnServerData": callback,
	           "fnRowCallback":rowCallBack,
	           "bFilter": false,
	           "bSort": false,
	           "sZeroRecords": "查询无记录！",
	           "bProcessing": true,
	           "oLanguage": {
	               "sUrl": '${ctx}/static/v2/js/plugins/datatables/cn.txt'
	            },
	             "aoColumns":[
	             	{"mData": null},
	             	 {"mData": "customer.customerName"},
	             	 {"mData": "brandName"},
	             	 {"mData": "bar"},
	             	 {"mData": "sku"},
	             	 {"mData": "skuName"},
	             	 {"mData": null},
	             	 {"mData": null},
	             	 {"mData": "tagPrice"},
	             	 {"mData": "weight"},
	             	 {"mData": "weightPkg"},
	             	 {"mData": "weightPop"},
	             	 {"mData": "len"},
	             	 {"mData": "wid"},
	             	 {"mData": "hei"},
	             	 {"mData": "spec"},
	             ],
	             "aoColumnDefs":[
	             	{"aTargets": [5],
		              "mRender": function (data, display, row) {
		              		return $("#roleFlg").val() == "1" ? ("<input type=\"text\" class=\"input-large\" id=\"skuName_"+row.id+"\"  value=\""+data+"\" onchange=\"skuNameChange('"+row.id+"');\"/>"+
				        		"<span  id='messageSkuName_"+row.id+"' ></span>") : data;
			           }
		            },
		            {"aTargets": [6],
			              "mRender": function (data, display, row) {
			              		return (row.isBatchMgmt == "1") ? "开启" : "关闭";
				           }
		            },
		            {"aTargets": [7],
			              "mRender": function (data, display, row) {
			              		return (row.isShelfLifeMgmt == "1") ? "开启" : "关闭";
				           }
			        },
	             	{"aTargets": [9],
		              "mRender": function (data, display, row) {
		              		return $("#roleFlg").val() == "1" ? ("<input type=\"text\" class=\"input-small\" id=\"weight_"+row.id+"\"  value=\""+data+"\" onchange=\"weightChange('"+row.id+"');\"/>"+
				        		"<span  id='message_"+row.id+"' ></span>") : data;
			           }
		            },
	             	{"aTargets": [10],
		              "mRender": function (data, display, row) {
		              		return $("#roleFlg").val() == "1" ? ("<input type=\"text\" class=\"input-small\" id=\"weightPkg_"+row.id+"\"  value=\""+data+"\" onchange=\"weightPkgChange('"+row.id+"');\"/>"+
				        		"<span  id='messagePkg_"+row.id+"' ></span>") : data;
			           }
		            },
	             ]
			});
		}
		
		function filter(item){
			filterParam[item.name] = item;
		}
		function callback(sSource, aoData, fnCallback ) {
		   	var formParam = $("#queryForm").serializeArray();
			for(var n in filterParam){
				formParam.push(filterParam[n]);
			}
		   $.ajax({ 
		       "type": "post",
		       "contentType": "application/json", 
		       "url": sSource,    
		       "dataType": "json",   
		       "data": JSON.stringify(aoData.concat(formParam)),
		       "success": fnCallback  
		   });
		}
		
		/**
		*创建了行的回调
		*nRow为当前的tr
		*/
		function rowCallBack(nRow, aData, iDisplayIndex, iDisplayIndexFull){
			var iDisplayStart = datatable_content.fnSettings()._iDisplayStart;
			$("td:eq(0)", nRow).html(iDisplayStart+iDisplayIndex+1);
		}
		
		function query(){
			if($("#queryForm").valid()){
				if (datatable_content) {
					datatable_content.fnDestroy();
				}
				initTable();
			}
		}
  
  //修改发货重量
  function weightChange(id){
     var weightId="#weight_"+id;
     var messageId="#message_"+id;
     
     var weight=$(weightId).val().trim();
     if(isNaN(weight)){
         $(messageId).css("color","red");
         $(messageId).html("请输入整数！");
         $(weightId).focus();
         $(weightId).select();
         return;
     }else{
        $(messageId).html("");
     }
     
     $.ajax({
		type : 'post',
		url : '${ctx}/goods/ajaxUpdateWeight',
		data : {
			goodsId:id,
			weight:weight
		},
		dataType : 'json',
		success : function(data) {
		    if(data==true){
		       $(messageId).css("color","green");
		       $(messageId).html("已修改");
		    }
		},
		error : function() {
			alert("网络繁忙，请稍后重试...");
		}
	});
  }
  
  //修改包装重量
  function weightPkgChange(id){
     var weightId="#weightPkg_"+id;
     var messageId="#messagePkg_"+id;
     
     var weight=$(weightId).val().trim();
     if(weight==''){
         $(messageId).css("color","red");
         $(messageId).html("请输入整数！");
         return;
     }
     
     if(isNaN(weight)){
         $(messageId).css("color","red");
         $(messageId).html("请输入整数！");
         $(weightId).focus();
         $(weightId).select();
         return;
     }else{
        $(messageId).html("");
     }
     
     $.ajax({
		type : 'post',
		url : '${ctx}/goods/ajaxUpdateWeightPkg',
		data : {
			goodsId:id,
			weight:weight
		},
		dataType : 'json',
		success : function(data) {
		    if(data==true){
		       $(messageId).css("color","green");
		       $(messageId).html("已修改");
		    }
		},
		error : function() {
			alert("网络繁忙，请稍后重试...");
		}
	});
  }
  
  //修改品名
  function skuNameChange(id){
     var weightId="#skuName_"+id;
     var messageId="#messageSkuName_"+id;
     
     var skuName=$(weightId).val().trim();
     if(skuName==''){
         $(messageId).css("color","red");
         $(messageId).html("请输入品名！");
         return;
     }
     else{
        $(messageId).html("");
     }
     
     $.ajax({
		type : 'post',
		url : '${ctx}/goods/ajaxUpdateSkuName',
		data : {
			goodsId:id,
			skuName:skuName
		},
		dataType : 'json',
		success : function(data) {
		    if(data==true){
		       $(messageId).css("color","green");
		       $(messageId).html("已修改");
		    }
		},
		error : function() {
			alert("网络繁忙，请稍后重试...");
		}
	});
  }
  
  function exportExcel(){
     $("#queryForm").attr("action","${ctx}/goods/exportExcel");
     $("#queryForm").submit();
  }
  
  function exportGoodsAge() {
	  $.ajax({
			type : 'post',
			url : '${ctx}/goods/exportGoodsAgeExcell',
			data : {
				"startDate":'',
				"endDate":'',
				"customerCode":'',
				"warehouseCode":''
			},
			dataType : 'json',
			success : function(data) {
			    alert("导出完成!");
			},
			error : function() {
				alert("网络繁忙，请稍后重试...");
			}
		});
  }
  
</script>
</head>

<body>
<section class="content">
<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-edit"></i>
					<h3 class="box-title">条件查询</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
				<form class="form-search" id="queryForm" method="post" >
							<div class="form-group row">
								<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<span class="input-group-text"> 商家： </span>
										 <select  name="_EQ_customerCode"  id="_customerCode" value="${param._EQ_customerCode}"  class="form-control required">
					<option value=""></option>
					<c:forEach items="${customerList}" var="customer">
						<option value="${customer.customerCode}"
						<c:if test="${param._EQ_customerCode eq customer.customerCode}">selected=selected</c:if>
						>${customer.customerName}</option>
					</c:forEach>
				</select>
									</div>
								</div>
								<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<span class="input-group-text"> 商品名称： </span>
										<input type="text" class="input-medium form-control" id="_skuName" name="_LIKE_skuName" value="${param._LIKE_skuName}"/>
									</div>
								</div>
								<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<span class="input-group-text"> 商品货号： </span> 
										<input type="text" class="input-medium form-control" id="_bar" name="_LIKE_bar" value="${param._LIKE_bar}"/>
									</div>
								</div>
								<div class="col-sm-3">
									<div class="input-group col-sm-12">
										<span class="input-group-text"> SKU： </span>
										<input type="text" class="input-medium form-control" id="_sku" name="_LIKE_sku" value="${param._LIKE_sku}"  onchange="query()"/>
									</div>
								</div>
							</div>
                               <div class="form-group row">
			 <div class="col-sm-12">
			    <button class="btn btn-primary" type="button"  onclick="query();">搜索</button>
			    <button class="btn btn-info" type="button" onclick="exportExcel();">导出Excel</button>
			 </div></div>
	  </form>
	</div>
	</div>
	</div>
	</div>
	
	<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-list"></i>
					<h3 class="box-title">查询结果</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
				<shiro:hasRole name="商品管理员">
					<input type="hidden" id="roleFlg" value="1"/>
				</shiro:hasRole>
				<shiro:lacksRole name="商品管理员">
					<input type="hidden" id="roleFlg" value="0"/>
				</shiro:lacksRole>
	<table id="contentTable" class="table table-striped table-bordered table-condensed">
		<thead><tr>					
			    <th style="width: 50px;">序号</th>
			    <th>商家</th>
			    <th>品牌</th>
			    <th>商品货号</th>
			    <th>SKU</th>
			    <th>商品名称</th>
			    <th>批次管理</th>
			    <th>效期管理</th>
			    <th>吊牌价</th>
			    <th>商品重量（g）</th>
			    <th>包装重量（g）</th>
			    <th>泡重</th>
			    <th>长</th>
			    <th>宽</th>
			    <th>高</th>
			    <th>规格参数</th>
			</tr></thead>
		<tbody>
		</tbody>
	</table>
	
	</div>
	</div>
	</div>
	</div>
	</section>
</body>
</html>
